/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package transactions;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import myJPA.Model;
import user.Customer;

/**
 *
 * @author Nacer Khalil
 */
public class CustomerList {
    
    public static boolean addCustomer(Customer customer)
    {
        if(customer!= null)
        {
            customer.save();
            return true;
        }
        return false;
    }
    
    public static boolean updateCustomer(int cusId, Customer customer)
    {
        Customer original = Model.getEntityManager().find(Customer.class, cusId);
        //Update First name
        if(!original.getFirstName().equals(customer.getFirstName()))
            original.setFirstName(customer.getFirstName());
        
        //Update last name
        if(!original.getLastName().equals(customer.getLastName()))
            original.setLastName(customer.getLastName());
        
        //Update Address
        if(!original.getAddress().equals(customer.getAddress()))
            original.setAddress(customer.getAddress());
        
        //Update phone number
        if(!original.getPhoneNumber().equals(customer.getPhoneNumber()))
            original.setPhoneNumber(customer.getPhoneNumber());
        
        //update registration date
        if(!original.getRegistrationDate().equals(customer.getRegistrationDate()))
            original.setRegistrationDate(customer.getRegistrationDate());
        
        original.update();
        
        return true;
    }
    
    public static boolean removeCustomer(Customer customer)
    {
        customer.remove();
        return true;
    }
    
    
    public static String produceBasicInfoReport()
    {
        Query query = Model.getEntityManager().createNamedQuery("getAllCustomers");
        List<Customer> list  = (List<Customer>) query.getResultList();
        String report = "";
        if(list == null)
            return report;
        for(Customer c: list)
        {
            report += displayInfo(c)+"\n";
        }
        return report;
    }
    
    public static String displayInfo (Customer Cust){
        
       Customer original = Model.getEntityManager().find(Customer.class, Cust.getId());
       String result = "Full name: "+original.getFullName() + ", phone number: " + original.getPhoneNumber()+", addresse: "+ original.getAddress() + ", registration date: " + original.getRegistrationDate();       
        
        return result; 
        
    }
    
    public static String producTotalRentedItemReport()
    {
        Query query = Model.getEntityManager().createNamedQuery("getAllCustomers");
        List<Customer> list  = (List<Customer>) query.getResultList();
        String report = "";
        if(list == null)
            return report;
        for(Customer c: list)
        {
            report += "Full name: "+c.getFullName()+", total rented: "+TotalItemRented(c)+"\n";
        }
        return report;
    }
       
   public static int TotalItemRented(Customer cust)
    {   
       
       int i,j=0; 
       
        Customer original = Model.getEntityManager().find(Customer.class, cust.getId());
       List<Rental> ls = original.getRentals();
       for(i=0; i< ls.size();i++){
          if (ls.get(i).isReturned() == false ){
              j++;          
          }
       }
    
       return j; 
       
    }
   
   
   
   
   
   
}
